package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.TagTypeMapper;
import com.iretailer.report.model.CompanyTagType;
import com.iretailer.report.model.MallTagType;
import com.iretailer.report.model.TagType;
import com.iretailer.report.model.TagTypeMalls;
import com.iretailer.report.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/7/18 0018 11:55
 * @Description:
 */
@Service
public class TagTypeService extends ServiceImpl<TagTypeMapper, TagType> {

    @Autowired
    private TagTypeMapper tagTypeMapper;
    @Autowired
    private TagTypeMallsService tagTypeMallsService;

    public PageResult gridTagType(List<String> companyIds, String companyName, String name, QueryOption queryOption) {
        Page page = new Page<CompanyTagType>(1, Long.MAX_VALUE);
        QueryWrapper<CompanyTagType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("1", 1);
        if (companyIds != null) {
            queryWrapper.in("companyId", companyIds);
        }
        if (companyName != null) {
            queryWrapper.like("companyName", companyName);
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("companyName").orderByAsc("ranked");
        IPage pageInfo = tagTypeMapper.selectPageTagType(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }


    public boolean deleteByIds(List<String> ids) {
        List<TagTypeMalls> tagTypeMalls = tagTypeMallsService.list(new QueryWrapper<TagTypeMalls>().lambda().in(TagTypeMalls::getTagTypeId, ids));
        if (tagTypeMalls != null && tagTypeMalls.size() > 0) {
            return false;
        } else {
            tagTypeMapper.deleteBatchIds(ids);
            return true;
        }
    }

    public boolean saveTagTypeMalls(String id, List<String> mallIds) {
        if (StringUtil.notEmpty(id) && mallIds != null) {
            tagTypeMallsService.remove(new QueryWrapper<TagTypeMalls>().lambda().eq(TagTypeMalls::getTagTypeId, id));
            if (mallIds.size() > 0) {
                List<TagTypeMalls> insertList = new ArrayList<>();
                for (String mallId : mallIds) {
                    insertList.add(new TagTypeMalls(id, mallId));
                }
                tagTypeMallsService.saveBatch(insertList);
            }
            return true;
        } else {
            return false;
        }
    }
}
